<?php
namespace Admin\Model;
use Think\Model;
class YuyueModel extends Model 
{
	protected $insertFields = array('course_id','member_id','yueyu_tel','yueyu_name','yueyu_comment','yueyu_time','add_time','company_id');
	protected $updateFields = array('id','course_id','member_id','yueyu_tel','yueyu_name','yueyu_comment','yueyu_time','add_time','company_id');
	protected $_validate = array(
		array('yueyu_tel', '1,255', '预约电话的值最长不能超过 255 个字符！', 2, 'length', 3),
        array('yueyu_tel', 'require', '预约电话不能为空！', 1, 'regex', 3),
        array('yueyu_name', 'require', '预约名字不能为空！', 1, 'regex', 3),
        array('yueyu_comment', 'require', '预约备注不能为空！', 1, 'regex', 3),
		array('yueyu_name', '1,255', '预约名字的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('yueyu_comment', '1,255', '预约备注的值最长不能超过 255 个字符！', 2, 'length', 3),

	);

	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($course_id = I('get.course_id'))
			$where['course_id'] = array('like', "%$course_id%");
		if($member_id = I('get.member_id'))
			$where['member_id'] = array('like', "%$member_id%");
		if($yueyu_tel = I('get.yueyu_tel'))
			$where['yueyu_tel'] = array('like', "%$yueyu_tel%");
		if($yueyu_name = I('get.yueyu_name'))
			$where['yueyu_name'] = array('like', "%$yueyu_name%");
			
		$start_timefrom = I('get.start_timefrom');
		$start_timeto = I('get.start_timeto');
		if($start_timefrom && $start_timeto)
			$where['yueyu_time'] = array('between', array(strtotime("$start_timefrom 00:00:00"), strtotime("$start_timeto 23:59:59")));
		elseif($start_timefrom)
			$where['yueyu_time'] = array('egt', strtotime("$start_timefrom 00:00:00"));
		elseif($start_timeto)
			$where['yueyu_time'] = array('elt', strtotime("$start_timeto 23:59:59"));
        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = ['eq',session('admin.company_id')];
        }
		/************************************* 翻页 ****************************************/
		if(I('id'))$where['id'] = I('id');
		/****************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->order('a.yueyu_time desc')->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	    if(empty($data['yueyu_time'])){
	        $data['yueyu_time'] = date('Y-m-d H:i:s',time());
        }
        $data['add_time'] =0;
        $data['company_id'] = session('admin.company_id');
        $userData['company_id'] = session('admin.company_id');
        //添加到散客
        $model = D('Admin/User');
        $userData['telphone'] = $data['yueyu_tel'];
        $where['telphone'] = $data['yueyu_tel'];
        $res = M('member')->where($where)->find();
        if(!$res)
        {
            $userData['username'] = $data['yueyu_name'];
            $userData['time'] = $data['yueyu_time'];
            $remarks = D('course')->select($data['course_id']);
            $userData['remarks'] = empty($data['yueyu_comment']) ? '预约体验:'.$remarks['course_name'] :$data['yueyu_comment'];
            $model->add($userData);
        }

    }
	// 修改前
	protected function _before_update(&$data, $option)
	{
        $data['company_id'] = session('admin.company_id');
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}